package com.neusoft.controller;

import com.neusoft.dto.EmpDto;
import com.neusoft.exception.EmpUpdateFail;
import com.neusoft.model.Emp;
import com.neusoft.service.AuthorityService;
import com.neusoft.service.EmpService;
import com.neusoft.utils.ServerResponse;
import com.neusoft.vo.EmpVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author cpx
 * @date 2025/1/8
 * @Description
 */
@RestController
@RequestMapping("/api")
public class EmpController {

    @Resource
    private EmpService empService;

    @Resource
    private AuthorityService authorityService;

    @PutMapping("/getEmp")
    public ServerResponse getEmp(Integer pageSize, Integer pageNum, @RequestBody EmpDto empDto) {
        return ServerResponse.success(empService.selectEmp(pageSize, pageNum, empDto));
    }

    @GetMapping("/getOneEmp")
    public ServerResponse getOneEmp(Integer id) {
        return ServerResponse.success(empService.selectEmpById(id));
    }

//    @PutMapping("/updateEmp")
//    public ServerResponse updateEmp(@RequestBody Emp emp) throws EmpUpdateFail {
//        Integer i = empService.updateEmp(emp);
//
//        if (i > 0) {
//            return ServerResponse.success("员工信息修改成功", i);
//        }
//        else {
//            throw new EmpUpdateFail(7001, "员工信息修改失败");
//        }
//    }

    @PutMapping("/updateStatusEmp")
    public ServerResponse updateStatusEmp(Integer id) throws EmpUpdateFail {
        Integer i = empService.updateStatus(id);

        if (i > 0) {
            return ServerResponse.success("员工信息修改成功", i);
        }
        else {
            throw new EmpUpdateFail(7001, "员工信息修改失败");
        }
    }

    @PostMapping("/insertOrUpdateEmp")
    public ServerResponse insertEmp(@RequestBody EmpVo empVo) {
        Integer i = empService.insertOrUpdateEmp(empVo);

        if (i > 0) {
            return ServerResponse.success("员工信息添加/修改成功", i);
        }
        else {
            throw new EmpUpdateFail(7002, "员工信息添加/修改失败");
        }
    }

    @GetMapping("/getAllAuthority")
    public ServerResponse getAllAuthority() {
        return ServerResponse.success(authorityService.getAllAuthority());
    }

    @GetMapping("/getEmpDept")
    public ServerResponse getEmpDept(Integer id) {
        return ServerResponse.success(empService.getEmpDept(id));
    }

    @GetMapping("/getEmpPosition")
    public ServerResponse getEmpPosition(Integer id) {
        return ServerResponse.success(empService.getEmpPosition(id));
    }

    @GetMapping("/getAllDept")
    public ServerResponse getAllDept() {
        return ServerResponse.success(empService.getAllDept());
    }

    @GetMapping("/getAllPosition")
    public ServerResponse getAllPosition() {
        return ServerResponse.success(empService.getAllPosition());
    }
}
